﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics.ColorSpaces;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Enhances the Color of the channels
    /// </summary>
    public class RGBEnhanceChannels : BaseImageFilter
    {
        protected ColorStruct m_nR = new ColorStruct(10,-10,-10);
        protected ColorStruct m_nG = new ColorStruct(-10, 10, -10);
        protected ColorStruct m_nB = new ColorStruct(-10, -10, 10);

        protected int m_nRedAmount = 10;
        protected int m_nGreenAmount = 10;
        protected int m_nBlueAmount = 10;

        /// <summary>
        /// Default constructor
        /// </summary>
        public RGBEnhanceChannels()
            : this(new ColorStruct(10, -10, -10), new ColorStruct(-10, 10, -10), new ColorStruct(-10, -10, 10))
        {
        }

        /// <summary>
        /// Primary constructor
        /// </summary>
        /// <param name="_nR"></param>
        /// <param name="_nG"></param>
        /// <param name="_nB"></param>
        public RGBEnhanceChannels(ColorStruct _nR, ColorStruct _nG, ColorStruct _nB)
        {
            m_nR = _nR;
            m_nG = _nG;
            m_nB = _nB;
        }

        /// <summary>
        /// Use this to clone this instance
        /// </summary>
        /// <param name="_enhanceChannels"></param>
        internal RGBEnhanceChannels(RGBEnhanceChannels _enhanceChannels)
        {
            this.m_nR = _enhanceChannels.m_nR;
            this.m_nG = _enhanceChannels.m_nG;
            this.m_nB = _enhanceChannels.m_nB;

            this.m_nRedAmount = _enhanceChannels.m_nRedAmount;
            this.m_nGreenAmount = _enhanceChannels.m_nGreenAmount;
            this.m_nBlueAmount = _enhanceChannels.m_nBlueAmount;
        }

        /// <summary>
        /// Gets or sets red amount
        /// </summary>
        public int RedAmount
        {
            get { return m_nRedAmount; }
            set { m_nRedAmount = value; }
        }

        /// <summary>
        /// Gets or sets green amount
        /// </summary>
        public int GreenAmount
        {
            get { return m_nGreenAmount; }
            set { m_nGreenAmount = value; }
        }

        /// <summary>
        /// Gets or sets blue amount
        /// </summary>
        public int BlueMount
        {
            get { return m_nBlueAmount; }
            set { m_nBlueAmount = value; }
        }

        /// <summary>
        /// Returns or gets R
        /// </summary>
        public ColorStruct R
        {
            get { return m_nR; }
            set { m_nR = value; }
        }

        /// <summary>
        /// Gets or sets G
        /// </summary>
        public ColorStruct G
        {
            get { return m_nG; }
            set { m_nG = value; }
        }

        /// <summary>
        /// Gets or sets B
        /// </summary>
        public ColorStruct B
        {
            get { return m_nB; }
            set { m_nB = value; }
        }

        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            for (int y = 0; y < nHeight; y++)
            {
                for (int x = 0; x < nWidth; x++)
                {
                    ColorStruct color = new ColorStruct(bitmap.GetPixel(x, y));
                    string sText = ColorFunctions.RGBMaxLetters(color.ToColor()).ToLower();
                    if (sText.Contains("r"))
                    {
                        color = new ColorStruct(color.R + m_nRedAmount, color.G - m_nRedAmount, color.B - m_nRedAmount);
                    }
                    if (sText.Contains("g"))
                    {
                        color = new ColorStruct(color.R - m_nGreenAmount, color.G + m_nGreenAmount, color.B - m_nGreenAmount);
                    }
                    if (sText.Contains("b"))
                    {
                        color = new ColorStruct(color.R - m_nBlueAmount, color.G - m_nBlueAmount, color.B + m_nBlueAmount);
                    }
                    color.CorrectLimits();
                    bitmap.SetPixel(x, y, color.ToColor());
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Use this to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new RGBEnhanceChannels(this);
        }
    }
}
